<template>
  <!-- 面包屑 -->
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="4">
          <div>
            <el-input v-model="queryParam.name" placeholder="请输入姓名" class="input-with-select" @clear="getWorkerList"
              clearable>
              <!-- <el-button @click="getWorkerList" slot="append" icon="el-icon-search"></el-button> -->
            </el-input>
          </div>
        </el-col>
        <el-col :span="5">
          <div>
            <el-input v-model="queryParam.mobile" placeholder="请输入手机号" class="input-with-select" @clear="getWorkerList"
              clearable>
              <!-- <el-button @click="getWorkerList" slot="append" icon="el-icon-search"></el-button> -->
            </el-input>
          </div>
        </el-col>
        <el-col :span="2">
          <el-button type="primary" @click="getWorkerList">搜索</el-button>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" @click="updateOrCreateProductClick('add')">添加工人</el-button>
        </el-col>
      </el-row>
      <template>
        <el-table :data="workerListData" style="width: 100%" stripe>
          <el-table-column fixed type="index"></el-table-column>
          <el-table-column fixed prop="name" label="姓名" width="70">
          </el-table-column>
          <el-table-column prop="face_path" label="照片" width="170">
            <template slot-scope="scope">
              <el-image style="height: 100%;" :src="scope.row.face_path" fit="fill"></el-image>
            </template>
          </el-table-column>
          <el-table-column prop="mobile" label="手机号" width="120">
          </el-table-column>
          <el-table-column prop="age" label="年龄" width="50">
          </el-table-column>
          <el-table-column prop="case_count" label="案例个数" width="80">
          </el-table-column>
          <el-table-column prop="work_age" label="工龄" width="50">
          </el-table-column>
          <el-table-column prop="id_number" label="身份证号" width="120">
          </el-table-column>
          <el-table-column prop="skills" label="技能" width="150">
          </el-table-column>
          <el-table-column prop="start" label="评分" width="150">
            <template slot-scope="scope">
              <el-rate v-model="scope.row.start" disabled show-score text-color="#ff9900" score-template="{value}">
              </el-rate>
            </template>
          </el-table-column>
          <el-table-column prop="status" label="状态" width="80">
            <template slot-scope="scope">
              <i style="color: lightgreen;" v-if="scope.row.status" class="el-icon-success"></i>
              <i style="color: red;" v-else class="el-icon-error"></i>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="150">
            <!--   -->
            <template slot-scope="scope">
              <el-tooltip class="item" effect="dark" content="查看" placement="top" :enterable="false">
                <el-button size="mini" type="warning" @click="updateOrCreateProductClick(scope.row.id)" icon="el-icon-view"
                  circle></el-button>
              </el-tooltip>
              <el-tooltip class="item" effect="dark" content="删除" placement="top" :enterable="false">
                <el-button @click="delWorkerClick(scope.$index,scope.row.id)" size="mini" type="danger" icon="el-icon-delete"
                  circle></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- 分页 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryParam.currentPage"
        :page-sizes="[10, 20, 30, 40]" :page-size="queryParam.pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="totalResult">
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
  import {
    _getWorkerListApi,
    _delWorkerApi
  } from '../request/api.js'
  export default {
    data() {
      return {
        workerListData: [],
        totalResult: 0,
        queryParam: {
          name: '',
          mobile: '',
          currentPage: 1,
          pageSize: 10
        }
      }
    },
    created() {
      this.getWorkerList()
    },
    methods: {
      async getWorkerList() {
        // 获取工人数据列表
        const {
          data: res
        } = await _getWorkerListApi(this.queryParam)
        console.log(res)
        this.workerListData = res.data.dataList
        this.totalResult = res.data.totalResult
      },
      // 分页事件
      handleSizeChange(newSize) {
        this.queryParam.pageSize = newSize
        this.queryParam.currentPage = 1
        this.getWorkerList()
      },
      // 状态变化事件
      handleCurrentChange(newPage) {
        this.queryParam.currentPage = newPage
        this.getWorkerList()
      },
      // 重置事件
      resetWorkerForm() {
        this.$refs.workerFormRef.resetFields()
      },
      updateOrCreateProductClick(workerId) {
        // 跳转到编辑页
        this.$router.push({
          name: 'worker_detail',
          query: {
            workerId: workerId
          }
        })
      },
      async delWorkerClick(index, id) {
        // 删除
        const data = await _delWorkerApi(id)
        if (data.status === 200 && data.data.status) {
          this.$message.success('删除成功！')
          this.workerListData.splice(index, 1)
        } else {
          this.$message.success('删除失败！')
        }
      }
    }
  }
</script>
<style>
  .el-pagination {
    text-align: center;
    margin-top: 15px;
  }
</style>
